package top.kaoshanji.p1ch10b;

import top.kaoshanji.p1ch09a.Note;

/**
 * 代码清单-p1ch10b-225页a：抽象类和抽象方法
 * author kaoshanji
 * ^_^
 * create time 2025/3/10 17:21
 */
public class Music4 {

    // 不关心类型，所以加入系统的新类型也能工作
    static void tune(Instrument instrument) {
        instrument.play(Note.MIDDLE_C);
    }

    static void tuneAll(Instrument[] e) {
        for (Instrument i: e) {
            tune(i);
        }
    }

    public static void main(String[] args) {

        // 在数组填充期间会向上转型
        Instrument[] orchestra = {new Wind(), new Percussion(), new Stringed(), new Brass(), new Woodwind()};

        tuneAll(orchestra);

    }

}

abstract class Instrument {
    private int i; // 该变量在每个对象里都会被分配存储
    public abstract void play(Note note); // 抽象方法只有方法声明

    // 一个普通方法
    public String what() {
        return "Instrument";
    }

    public abstract void adjust();

}

class Wind extends Instrument {

    @Override
    public void play(Note note) {
        System.out.println("Wind.play() " + note);
    }

    @Override
    public String what() {
        return "Wind";
    }

    @Override
    public void adjust() {
        System.out.println("Adjusting Wind");
    }
}

class Percussion extends Instrument {

    @Override
    public void play(Note note) {
        System.out.println("Percussion.play() " + note);
    }

    @Override
    public String what() {
        return "Percussion";
    }

    @Override
    public void adjust() {
        System.out.println("Adjusting Percussion");
    }
}

class Stringed extends Instrument {

    @Override
    public void play(Note note) {
        System.out.println("Stringed.play() " + note);
    }

    @Override
    public String what() {
        return "Stringed";
    }

    @Override
    public void adjust() {
        System.out.println("Adjusting Stringed");
    }
}

class Brass extends Wind {

    @Override
    public void play(Note note) {
        System.out.println("Brass.play() " + note);
    }

    @Override
    public void adjust() {
        System.out.println("Adjusting Brass");
    }
}


class Woodwind extends Wind {

    @Override
    public void play(Note note) {
        System.out.println("Woodwind.play() " + note);
    }

    @Override
    public String what() {
        return "Woodwind";
    }
}
